﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
using System.Threading.Tasks;
using SWCInvoice.Model;
using SWCInvoice.Utils;
using SWCInvoice.Returntyps;

namespace SWCInvoice.Servicelayer {
    public class Worker {
        private static MainWindow _mainWindow;

        public Worker() {
            
        }

        /// <summary>
        /// Wrapper for the ExtractPDF
        /// </summary>
        /// <param name="fileName">The filename of the PDF file</param>
        public static void ExtractPDF(string fileName) {
            ExtractPDFInfo extractPDFInfo = null;

            _mainWindow = (MainWindow)Application.Current.MainWindow;

            //int SecondsToComplete = 30; //Program specific value
            //Duration duration = new Duration(TimeSpan.FromSeconds((SecondsToComplete * 1.35)));
            //DoubleAnimation doubleAnimatiion = new DoubleAnimation(200, duration);

            try {
                TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();

                Action MainThreadDoWork = new Action(() => {
                    extractPDFInfo = PDFHandler.ExtractPDF(fileName);
                });

                //Action ExecuteProgressBar = new Action(() => {
                //    _MainWindow.progressBar.BeginAnimation(System.Windows.Controls.ProgressBar.ValueProperty, doubleAnimatiion);
                //});

                Action FinalThreadDoWOrk = new Action(() => {
                    //_MainWindow.progressBar.Value = 0;                    
                    if (extractPDFInfo.VirtualDocumentStructure != null && extractPDFInfo.VirtualDocumentStructure.Documents != null && extractPDFInfo.VirtualDocumentStructure.Documents.Count > 0) {
                        _mainWindow.MainViewModel.VirtualDocumentStructureViewModel.VirtualDocumentStructures.Add(extractPDFInfo.VirtualDocumentStructure);
                        _mainWindow.MainViewModel.VirtualDocumentStructureViewModel.CurrentVirtualDocumentStructure = _mainWindow.MainViewModel.VirtualDocumentStructureViewModel.VirtualDocumentStructures[0];
                    }
                    _mainWindow.ResetIsBusy();
                    _mainWindow.MainViewModel.ClearMessageQueueCommand.Execute(null);
                    _mainWindow.MainViewModel.AddMessageCommand.Execute(extractPDFInfo.GetMessage());
                });

                _mainWindow.SetIsBusy(Properties.ResourceMain.ProgressLoadDocuments);
                Task MainThreadDoWorkTask = Task.Factory.StartNew(() => MainThreadDoWork());

                //Task ExecuteProgressBarTask = new Task(ExecuteProgressBar);
                //ExecuteProgressBarTask.RunSynchronously();

                MainThreadDoWorkTask.ContinueWith(t => FinalThreadDoWOrk(), uiThread);
            } catch (Exception ex) {
                System.Windows.MessageBox.Show("Error", ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// Wrapper of the CreatePDF
        /// </summary>
        /// <param name="vds">The virtual document structure</param>
        /// <param name="withoutOriginalDocuments">Create only documents with the the isOriginal flag = false</param>
        public static void CreatePDF(VirtualDocumentStructure vds, bool withoutOriginalDocuments = true) {
            CreatePDFInfo createPDFInfo = null;

            _mainWindow = (MainWindow)Application.Current.MainWindow;           

            try {
                TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();

                Action MainThreadDoWork = new Action(() => {
                    createPDFInfo = PDFHandler.CreatePDF(vds, false);
                });

                Action FinalThreadDoWOrk = new Action(() => {

                    _mainWindow.ResetIsBusy();
                    _mainWindow.MainViewModel.ClearMessageQueueCommand.Execute(null);
                    _mainWindow.MainViewModel.AddMessageCommand.Execute(createPDFInfo.GetMessage());
                });

                _mainWindow.SetIsBusy();
                Task MainThreadDoWorkTask = Task.Factory.StartNew(() => MainThreadDoWork());

                MainThreadDoWorkTask.ContinueWith(t => FinalThreadDoWOrk(), uiThread);
            } catch (Exception ex) {
                System.Windows.MessageBox.Show("Error", ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            } 
        }
        /// <summary>
        /// Wrapper of the SetPageIdentifier
        /// </summary>
        /// <param name="identifier">The used identigier</param>
        public static void SetPageIdentifier(Identifier identifier) {            
            _mainWindow = (MainWindow)Application.Current.MainWindow;
            VirtualDocumentStructure vds = _mainWindow.MainViewModel.VirtualDocumentStructureViewModel.VirtualDocumentStructures[0];

            try {
                TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();

                Action MainThreadDoWork = new Action(() => {
                    PDFHandler.SetPageIdentifier(_mainWindow.MainViewModel.VirtualDocumentStructureViewModel.VirtualDocumentStructures[0], identifier.SearchKeyword, identifier.Expression, identifier.Inheritance, identifier.Additional);
                });

                Action FinalThreadDoWOrk = new Action(() => {
                    _mainWindow.ResetIsBusy();
                });

                _mainWindow.SetIsBusy();
                Task MainThreadDoWorkTask = Task.Factory.StartNew(() => MainThreadDoWork());

                MainThreadDoWorkTask.ContinueWith(t => FinalThreadDoWOrk(), uiThread);
            } catch (Exception ex) {
                System.Windows.MessageBox.Show("Error", ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            } 
        }

        /// <summary>
        /// Wrapper of the SplitDocumentByIdentifier
        /// </summary>
        /// <param name="baseDocument"></param>
        public static void SplitDocumentByIdentifier(Document baseDocument) {
            _mainWindow = (MainWindow)Application.Current.MainWindow;
            VirtualDocumentStructure vds = _mainWindow.MainViewModel.VirtualDocumentStructureViewModel.VirtualDocumentStructures[0];

            try {
                TaskScheduler uiThread = TaskScheduler.FromCurrentSynchronizationContext();

                Action MainThreadDoWork = new Action(() => {
                    PDFHandler.SplitDocumentByIdentifier(baseDocument);
                });

                Action FinalThreadDoWOrk = new Action(() => {
                    _mainWindow.ResetIsBusy();                    
                });

                _mainWindow.SetIsBusy();
                Task MainThreadDoWorkTask = Task.Factory.StartNew(() => MainThreadDoWork());

                MainThreadDoWorkTask.ContinueWith(t => FinalThreadDoWOrk(), uiThread);
            } catch (Exception ex) {
                System.Windows.MessageBox.Show("Error", ex.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            } 
        }
    }
}
